package cn.keniykey.mc.landterritory.landflag.flag;

import java.util.Arrays;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockFormEvent;
import org.bukkit.event.block.BlockGrowEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.block.LeavesDecayEvent;
import org.bukkit.event.world.StructureGrowEvent;

import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.api.ILandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.land.api.LandUtil;
import cn.keniykey.mc.landterritory.land.api.LandUtil.ELandPermissionLevel;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagParType;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagType;
import cn.keniykey.mc.landterritory.landflag.api.ILandFlagDetection;
import cn.keniykey.mc.landterritory.landflag.api.JavaLandFlag;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagListenerManager;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagSetting;
import cn.keniykey.mc.landterritory.toolkit.Util;
import cn.keniykey.mc.landterritory.vault.Perm;

public final class BanPhysicalFlag extends JavaLandFlag implements Listener,ILandFlagDetection{
	private String per_add;
	private ELandFlagType type;
	
	@Override
	public ELandFlagType getELandFlagType() {
		return this.type;
	}
	@Override
	public ELandFlagParType getELandFlagParType() {
		return ELandFlagParType.MUCH;
	}
	@Override
	public boolean allowAllPlayerData() {
		return false;
	}
	
	@Override
	public void onEnable() {
		LandFlagListenerManager.registerListener(this, this);
		LandFlagSetting.setAddDetection(this, this);
	}
	@EventHandler
	public void onConfigLoad(final LandTerritoryReloadConfigEvent e) {
		final LandFlagConfigurationFile config = this.getLandFlagConfigurationFile();
		if(!config.has()) {
			config.set("name", "禁止方块物理现象");
			config.set("lore", Arrays.asList("&7（禁止领地内的方块物理现象）","&7（以下为变量参数）", "&b[burn：烧毁]，[fade：融化]，[form：形成]",
					"&b[grow：生成]，[ignite：着火]，[spread：蔓延]","&b[decay：树叶掉落]，[structure：树苗生长]，[physics：方块物理]","&5（标记参数为不禁止的类型）"));
			config.set("per", "land.flag." + this.getName());
		}
		this.setDisplayName(Util.conver(config.getString("name")));
		this.setIntroductionLore(Util.conver(config.getStringList("lore")));
		this.per_add = config.getString("per");
		if(this.per_add == null || this.per_add.isEmpty()) {
			this.type = ELandFlagType.PLAYER;
		}else if(this.per_add.equals(ConfigurationManager.AdminPer)) {
			this.type = ELandFlagType.ADMIN;
		}else {
			this.type = ELandFlagType.SPECIAL;
		}
	}
	
	private void onCancellable(final String str,final Location loc,final Cancellable able) {
		final Land land = ILandManager.getILandManager().getHighestPriorityLand(loc);
		if(land == null) return;
		if(!JavaLandFlag.hasLandFlag(land, this)) return;
		if(JavaLandFlag.hasLandFlagParameter(land, this, str)) return;
		able.setCancelled(true);
	}
	
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onBlockBurn(final BlockBurnEvent e) {//烧毁
		this.onCancellable("burn", e.getBlock().getLocation(), e);
	}
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onBlockFade(final BlockFadeEvent e) {//融化
		this.onCancellable("fade", e.getBlock().getLocation(), e);
	}
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onBlockForm(final BlockFormEvent e) {//形成
		this.onCancellable("form", e.getBlock().getLocation(), e);
	}
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onBlockGrow(final BlockGrowEvent e) {//生成
		this.onCancellable("grow", e.getBlock().getLocation(), e);
	}
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onBlockIgnite(final BlockIgniteEvent e) {//着火
		this.onCancellable("ignite", e.getBlock().getLocation(), e);
	}
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onBlockSpread(final BlockSpreadEvent e) {//蔓延
		this.onCancellable("spread", e.getSource().getLocation(), e);
	}
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onLeavesDecay(final LeavesDecayEvent e) {//树叶掉落
		this.onCancellable("decay", e.getBlock().getLocation(), e);
	}
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onStructureGrow(final StructureGrowEvent e) {//树苗生长
		this.onCancellable("structure", e.getLocation(), e);
	}
	@EventHandler(priority = EventPriority.NORMAL,ignoreCancelled = true)
	public void onBlockPhysics(final BlockPhysicsEvent e) {//方块物理，估计是重力下坠，例如砂砾沙子
		this.onCancellable("physics", e.getBlock().getLocation(), e);
	}
	
	@Override
	public boolean onRequest(Player player, Land land) {
		switch(this.type) {
		case PLAYER:
			return true;
		case ADMIN:
			return LandUtil.hasLandPermission(player, land, ELandPermissionLevel.ADMIN);
		default:
		}
		return Perm.getPerm().hasPermission(land.getWorldName(), player, this.per_add);
	}
}